package com.dldata.devtools30.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.script.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义表单校验格式
 * function fo(){
 *   return result=[name] !=null;
 * }
 */
public class DlScriptEngine {

    private final static Logger logger = LoggerFactory.getLogger(DlScriptEngine.class);

    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        List<ScriptEngineFactory> factories = manager.getEngineFactories();
        for (ScriptEngineFactory factory : factories){
            System.out.println(factory.getNames());
        }
        ScriptEngine scriptEngine=manager.getEngineByName("javascript");
        scriptEngine.eval("result=/^[\\u0391-\\uFFE5]+$/.test('中文')");
        Object obj= scriptEngine.get("result");
        System.out.println("scriptEngine: "+obj);

//        getJavaScriptResult("result='111' !=null");
//        getJavaScriptResult("result=[name] !=null");
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("function fo(){").append("\r\n");
        stringBuilder.append(" var value='中文';").append("\r\n");
        stringBuilder.append(" var valueTrim=value.replace(\"/^(\\s|\\u00A0)+|(\\s|\\u00A0)+$/g\", \"\" ); ").append("\r\n");
        stringBuilder.append(" return /^[\\u0391-\\uFFE5]+$/.test(valueTrim);").append("\r\n");
        stringBuilder.append("}").append("\r\n");

        System.out.println("testJavaScript: "+testJavaScript(stringBuilder.toString()));
        System.out.println("getJavaScriptResult: "+getJavaScriptResult(stringBuilder.toString()));

//        Pattern pattern = Pattern.compile(Public.REG_FIELD_ZH);
//        Matcher matcher1=pattern.matcher("//[测试主表Demo.名称] !=null;");
        Pattern pattern = Pattern.compile("/^[\\\\u0391-\\\\uFFE5]+$/");
        Matcher matcher1=pattern.matcher("中文");
        System.out.println(matcher1.find());
//        System.out.println(matcher1.group());
//        System.out.println(matcher1.groupCount());


    }
    /**
     * 校验参数,返回脚本执行的result值
     * @param content
     * @return 脚本执行的result值
     */
    public final static Boolean getJavaScriptResult(String content){
        if(!content.trim().startsWith("function fo(){")||!content.trim().endsWith("}")){
            return Boolean.FALSE;
        }
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine scriptEngine=manager.getEngineByName("javascript");
        Object obj =null;
        try {
            obj = scriptEngine.eval(content);
            Invocable invocableEngine = (Invocable) scriptEngine;
            obj = invocableEngine.invokeFunction("fo",new Object());
        } catch (Exception e) {
            logger.error(e.getMessage()+" "+content,e);
        }
        if(obj==null||!(obj instanceof Boolean)){
            return Boolean.FALSE;
        }
        return (Boolean)obj;
    }


    /**
     * 校验脚本格式是否正确
     * @param content
     * @return
     */
    public final static Boolean testJavaScript(String content){

        if(!content.trim().startsWith("function fo(){")||!content.trim().endsWith("}")){
            return Boolean.FALSE;
        }
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine scriptEngine=manager.getEngineByName("javascript");
        Object obj =null;
        try {
            scriptEngine.eval(content);
            Invocable invocableEngine = (Invocable) scriptEngine;
            obj = invocableEngine.invokeFunction("fo",new Object());
        } catch (Exception e) {
            logger.error(e.getMessage()+" "+content,e);
        }
        if(obj==null||!(obj instanceof Boolean)){
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

}
